package cn.innosmart.aq.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import cn.innosmart.aq.application.AQApplication;
import cn.innosmart.aq.bean.BoxBean;
import cn.innosmart.aq.bean.Reminder;

/**
 * Created by David on 2015/7/21.
 */
public class SharedUtil {
    private Context context = AQApplication.getInstance();
    private String Count = "CountInfo";
    private String Project = "AQProject";
    private String Wifi = "Wifi";

    private SharedUtil() {

    }

    public static SharedUtil getInstance() {
        return SharedUtilInstance.sharedUtil;
    }

    public void writeLoginSession(String session_token) {
        SharedPreferences loginSp = context.getSharedPreferences(Count, Context.MODE_PRIVATE);
        loginSp.edit().putString("session_token", session_token).commit();
    }
//    private String PushMessage = "AQPush";

    public JSONObject readLoginInfo() {
        SharedPreferences loginSp = context.getSharedPreferences(Count, Context.MODE_PRIVATE);
        String username = loginSp.getString("username", "");
        String password = loginSp.getString("password", "");
        JSONObject jsonLogin = new JSONObject();
        try {
            jsonLogin.put("username", username);
            jsonLogin.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonLogin;
    }

    /**
     * @param username
     * @param password
     */
    public void writeLoginInfo(String username, String password) {
        SharedPreferences loginSp = context.getSharedPreferences(Count, Context.MODE_APPEND);
        System.out.println("password=" + password);
        loginSp.edit().putString("username", username).commit();
        loginSp.edit().putString("password", password).commit();
    }

    public void updateLoginPassword(String password) {
        SharedPreferences loginSp = context.getSharedPreferences(Count, Context.MODE_APPEND);
        System.out.println("password=" + password);
        loginSp.edit().putString("password", password).commit();
    }

    /**
     * 清楚账户信息，注销时调用
     */
    public void clearLoginInfo() {
        SharedPreferences loginSp = context.getSharedPreferences(Count, Context.MODE_PRIVATE);
        loginSp.edit().remove("password").commit();
        loginSp.edit().commit();
    }

    public void writeGatewayInfo(String gatewayinfo) {
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        gatewayInfoSp.edit().putString("boxlistinfo", gatewayinfo).commit();
    }

    public void clearGatewayInfo() {
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        gatewayInfoSp.edit().clear().commit();
    }

    public void writeUserInfo(String userinfo) {
        SharedPreferences userInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        userInfoSp.edit().putString("userinfo", userinfo).commit();
    }

    public JSONObject readUserInfo() {
        SharedPreferences userInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        String userInfo = userInfoSp.getString("userinfo", "");
        JSONObject jsonLogin;
        try {
            jsonLogin = new JSONObject(userInfo);
        } catch (JSONException e) {
            e.printStackTrace();
            jsonLogin = new JSONObject();
        }
        return jsonLogin;
//        userInfoSp.getJSOn.putString("userinfo", userinfo).commit();
    }

    public void writeLastConnect(String did) {
        SharedPreferences lastConnectSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        lastConnectSp.edit().putString("lastdid", did).commit();
    }

    public String readLastConnect() {
        SharedPreferences lastConnectSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        String lastdid = lastConnectSp.getString("lastdid", null);
        return lastdid;
    }

    public String readUserId() {
        SharedPreferences userInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        String userInfo = userInfoSp.getString("userinfo", null);
        JSONObject jsonObject = new JSONObject();
        String userid = null;
        if (userInfo != null) {
            try {
                jsonObject = new JSONObject(userInfo);
                userid = jsonObject.getString("id");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return userid;
    }

    public String readApiKey() {
        SharedPreferences userInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        String userInfo = userInfoSp.getString("userinfo", null);
        JSONObject jsonObject = new JSONObject();
        String apikey = null;
        if (userInfo != null) {
            try {
                jsonObject = new JSONObject(userInfo);
                apikey = jsonObject.getString("apikey");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return apikey;
    }

    /**
     * @return
     */
    public String readGatewayInfo() {
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        return gatewayInfoSp.getString("boxlistinfo", null);
    }

    /**
     * 读取本地Gateway列表信息
     *
     * @return
     */
    public String readLocalGatewayInfo() {
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        return gatewayInfoSp.getString("localboxlistinfo", null);
    }

    /**
     * 写入本地gateway信息
     *
     * @param list
     */
    public void writeLocalGatewayInfo(ArrayList<BoxBean> list) {
        String gatewayinfo = JSON.toJSONString(list);
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        gatewayInfoSp.edit().putString("localboxlistinfo", gatewayinfo).commit();
    }

    /**
     * 清除本地Gateway列表信息
     *
     * @return
     */
    public void clearLocalGatewayInfo() {
        SharedPreferences gatewayInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        gatewayInfoSp.edit().remove("localboxlistinfo").commit();
    }

    /**
     * @return
     */
    public String readBannerInfo() {
        SharedPreferences banerInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        return banerInfoSp.getString("bannerInfo", null);
    }

    /**
     * @param bannerInfo
     */
    public void writeBannerInfo(String bannerInfo) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(Project, Context.MODE_PRIVATE);
        banerInfoSp.edit().putString("bannerInfo", bannerInfo).commit();
    }

    public void writeDefaultSubscription(boolean isSubscribed, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putBoolean("defaultSubscription", isSubscribed).commit();
    }

    public boolean readDefaultSubscription(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getBoolean("defaultSubscription", false);
    }

    public void writeXinStatus(boolean isSubscribed, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putBoolean("xinge", isSubscribed).commit();
    }

    public boolean readXinStatus(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getBoolean("xinge", false);
    }

    public void writeWXStatus(boolean isSubscribed, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putBoolean("wx", isSubscribed).commit();
    }

    public boolean readWXStatus(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getBoolean("wx", false);
    }

    public void writeTelStatus(boolean isSubscribed, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putBoolean("tel", isSubscribed).commit();
    }

    public boolean readTelStatus(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getBoolean("tel", false);
    }

    public void writeTelNo(String telno, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putString("telno", telno).commit();
    }

    public String readTelNo(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getString("telno", null);
    }

    public void writeSmsStatus(boolean isSubscribed, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putBoolean("sms", isSubscribed).commit();
    }

    public boolean readSmsStatus(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getBoolean("sms", false);
    }

    public void writeSmsNo(String telno, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putString("smsno", telno).commit();
    }

    public String readSmsNo(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getString("smsno", null);
    }

    public void writeWorkLog(String workLog, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putString("worklog", workLog).commit();
    }

    public String readWorkLog(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getString("worklog", null);
    }

    public void writeTemp(String workLog, String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        banerInfoSp.edit().putString("temp", workLog).commit();
    }

    public String readTemp(String uid) {
        SharedPreferences banerInfoSp = context.getSharedPreferences(uid, Context.MODE_PRIVATE);
        return banerInfoSp.getString("temp", null);
    }

    public ArrayList<Reminder> queryAllReminder() {
        SharedPreferences reminderSp = context.getSharedPreferences("Reminder", Context.MODE_APPEND);
        ArrayList<Reminder> reminders = new ArrayList<>();
        Reminder reminder = null;
        Map<String, ?> maps = reminderSp.getAll();
        Iterator iterator = maps.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            reminder = JSON.parseObject((String) entry.getValue(), Reminder.class);
            reminders.add(reminder);
        }

        return reminders;
    }

    public Reminder queryReminder(String title) {
        SharedPreferences reminderSp = context.getSharedPreferences("Reminder", Context.MODE_APPEND);
        String reminderJson = reminderSp.getString("title", null);
        Reminder reminder = JSON.parseObject(reminderJson, Reminder.class);
        return reminder;
    }

    /**
     * 根据id来添加备忘提醒
     *
     * @param reminder
     */
    public void addReminder(Reminder reminder) {
        SharedPreferences reminderSp = context.getSharedPreferences("Reminder", Context.MODE_PRIVATE);
        reminder.id = reminderSp.getAll().size() + 1;
        String reminderJson = JSON.toJSONString(reminder);
        reminderSp.edit().putString(reminder.id + "", reminderJson).commit();
    }

    public void updateReminder(Reminder reminder) {
        SharedPreferences reminderSp = context.getSharedPreferences("Reminder", Context.MODE_PRIVATE);
        String reminderJson = JSON.toJSONString(reminder);
        reminderSp.edit().putString(reminder.id + "", reminderJson).commit();
    }

    public void removeReminder(int id) {
        SharedPreferences reminderSp = context.getSharedPreferences("Reminder", Context.MODE_PRIVATE);
        reminderSp.edit().remove(id + "").commit();
    }

    public void writeWifiInfo(String ssid, JSONObject jsonParams) {
        SharedPreferences wifiSp = context.getSharedPreferences(Wifi, Context.MODE_PRIVATE);
        wifiSp.edit().putString(ssid, jsonParams.toString()).commit();
    }

    public JSONObject readWifiInfo(String ssid) {
        SharedPreferences wifiSp = context.getSharedPreferences(Wifi, Context.MODE_PRIVATE);
        String wifi = wifiSp.getString(ssid, "");
        JSONObject jsonWifi = null;
        try {
            jsonWifi = new JSONObject(wifi);
        } catch (JSONException e) {
//            e.printStackTrace();
        }
        return jsonWifi;
    }


    private static class SharedUtilInstance {
        private static SharedUtil sharedUtil = new SharedUtil();
    }
}
